//
//  TimerHolder.m
//  yixin_iphone
//
//  Created by lwc on 14-3-14.
//  Copyright (c) 2013年 Netease. All rights reserved.
//

#import "TimerHolder.h"

@interface TimerHolder ()
{
    NSTimer *_holderTimer;
    BOOL    _repeats;
}
- (void)onTimer: (NSTimer *)timer;
@end

@implementation TimerHolder

- (void)dealloc
{
    [self stopTimer];
}

- (void)startTimer: (NSTimeInterval)seconds
          delegate: (id<TimerHolderDelegate>)timerHolderDelegate
           repeats: (BOOL)repeats
{
    _delegate = timerHolderDelegate;
    _repeats = repeats;
    if (_holderTimer)
    {
        [_holderTimer invalidate];
        _holderTimer = nil;
    }
    _holderTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
                                                    target:self
                                                  selector:@selector(onTimer:)
                                                  userInfo:nil
                                                   repeats:repeats];
}

- (void)stopTimer
{
    [_holderTimer invalidate];
    _holderTimer = nil;
    _delegate = nil;
}

#pragma mark - misc
- (void)onTimer: (NSTimer *)timer
{
    if (!_repeats)
    {
        _holderTimer = nil;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(onTimerHolderFire:)])
    {
        [_delegate onTimerHolderFire:self];
    }

}

@end
